//
//  XWAFNetworkTool.m
//  Test
//
//  Created by 奚旺 on 15/11/6.
//  Copyright (c) 2015年 奚旺. All rights reserved.
//

#import "XWAFNetworkTool.h"
#import "AFNetworking.h"

@implementation XWAFNetworkTool
+(void)getUrl:(NSString *)url body:(id)body response:(XWResposeStyle)style
requestHeadFile:(NSDictionary *)headFile success:(void (^)(NSURLSessionDataTask *task, id resposeObject))success
      failure:(void (^)(NSURLSessionDataTask * task,NSError * error))failure
{
    //1获取单例的网络管理对象
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];

    // 2,根据style的类型，取选择返回值类型
    switch (style) {
        case XWJSON:
            manager.responseSerializer =[AFJSONResponseSerializer serializer];
            break;
            case XWXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
            case XWData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
           
            
        default:
            break;
    }
    if (headFile) {
        
        for (NSString * key in headFile.allKeys) {
             [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
        }
        
        
        
    }

    // 转码
    // 3. 设置响应数据支持类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"application/x-www-form-urlencoded",@"application/x-javascript", nil]];
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:
           [NSCharacterSet URLQueryAllowedCharacterSet]];
        //  3,发送get请求
 
    [manager GET:url parameters:body success:^(NSURLSessionDataTask *task, id responseObject) {
        
        /* ************************************************** */
        //如果请求成功 , 回调请求到的数据 , 同时 在这里 做本地缓存
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        
        
        /* if判断， 防止success 为空 */
        if (success) {
            
            success(task, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (error) {
            
            /* *************************************************** */
            // 在这里读取本地缓存
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
            
            if (result) {
                success(task, result);
            }else{
                failure(task,error);
            }
            
            //            failure(task, error);
        }
        
    }];
}
+ (void)postUrlString:(NSString *)url
body:(id)body
response:(XWResposeStyle)style
bodyStyle:(XWRequestStyle)bodyStyle
requestHeadFile:(NSDictionary *)headFile
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
    {
        // 1.获取Session管理对象
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        // 2.设置网络请求返回时, responseObject的数据类型
        switch (style) {
            case XWJSON:
                manager.responseSerializer = [AFJSONResponseSerializer serializer];
                break;
            case XWXML:
                manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
                break;
            case XWData:
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                break;
            default:
                break;
        }
        // 3.判断body体的类型
        switch (bodyStyle) {
            case XWRequestJSON:
                // 以JSON格式发送
                manager.requestSerializer = [AFHTTPRequestSerializer serializer];
                break;
            case XWRequestString:
                // 保持字符串样式
                [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
                    return parameters;
                }];
            case XWRequestDefault:
                // 默认情况会把JSON拼接成字符串, 但是没有顺序
                break;
                
            default:
                break;
        }
        // 4.给网络请求加请求头
        if (headFile) {
            for (NSString *key in headFile.allKeys) {
                [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
            }
        }
        // 5.设置支持的响应头格式
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript", nil]];
        // 6.发送网络请求
        [manager GET:url parameters:body success:^(NSURLSessionDataTask *task, id responseObject) {
            if (success) {
                success(task, responseObject);
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            if (failure) {
                failure(task, error);
            }
        }];
        
        
        
    }

    
    
    

@end
